﻿using System;
 

namespace DemoApp1
{
    /// <summary>
    /// 循环
    /// </summary>
    public class LoopDemo
    {
        public string DemoString = "1234567890abcdefghi";
        public int[] Numbers = new int[] {1, 3, 7, 9, 11};

        private const int V = 21;

        /// <summary>
        /// for 样例
        /// </summary>
        public void ShowFor()
        {
            Console.WriteLine();
            Console.WriteLine("for");
            for (var i = 0; i < DemoString.Length; i++)
            {
                char c = DemoString[i];
                Console.WriteLine("{0}:{1}", i, c);
            }
        }

        /// <summary>
        /// foreach 样例
        /// </summary>
        public void ShowForeach()
        {
            Console.WriteLine();
            Console.WriteLine("foreach");
            foreach (var n in Numbers)
            {
                Console.WriteLine(n);
            }
        }

        /// <summary>
        /// while 样例
        /// </summary>
        public void ShowWhile()
        {
            Console.WriteLine();
            Console.WriteLine("while 求100以内{0}的倍数", V);
            var i = 0;
            while ( i < 100)
            {
                if (i % V == 0)
                {
                    Console.WriteLine(i);
                }
                i++;
            }
        }

        /// <summary>
        /// do while 样例
        /// </summary>
        public void ShowDoWhile()
        {
            Console.WriteLine();
            Console.WriteLine("do while 求100以内{0}的倍数", V);
            var i = 0;
            do
            {
                if (i % V == 0)
                {
                    Console.WriteLine(i);
                }
                i++;
            } while (i < 100);
        }

        /// <summary>
        /// break continue 的区别
        /// </summary>

        public void ContinutBreak()
        {
            Console.WriteLine();
            Console.WriteLine("break continue 的区别");
            // 产生 随机数 -------------Start ------------------
            var arr = new int[10];
            var i = 0;
            var random = new Random();
            while (i < arr.Length)
            {
                arr[i] = random.Next(1, 100);
                i++;
            }
            // 产生 随机数 -------------End --------------------
            Console.WriteLine();
            Console.WriteLine("continue 遇到奇数 跳过本次，执行下次循环");

            foreach (var n in arr)
            {
                Console.Write(n);
                if (n % 2 == 1)
                {
                    Console.WriteLine(" 奇数");
                    continue;
                }
                Console.WriteLine("……");
            }

            Console.WriteLine();
            Console.WriteLine("break 遇到奇数停止 循环程序");
            foreach (var n in arr)
            {
                Console.Write(n);
                if (n % 2 == 1)
                {
                    Console.WriteLine(" 奇数");
                    break;
                }
                Console.WriteLine("……");
            }

        }
    }
}
